//
//  GLSLViewController.swift
//  ZSceneDemo
//
//  Created by 张鑫 on 2020/9/8.
//  Copyright © 2020 www.lionaitech.com. All rights reserved.
//

import UIKit

import SceneKit

class GeometrySourceViewController: UIViewController {
    
    // OpenGL Shader Language一种着色器语言,我们可以自定义的程序片段,它在GPU 上执行,代替了固定的渲染管线的一部分,它由片段着色器和顶点着色器组成
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadModelFile()
        
        //        shaderModifiers()
        //        sourceAndElement()
//        simpleSourceAndElement()
        tetrahedron()
    }
    
    // 手势点击节点
    func tapHandle(gesture:UITapGestureRecognizer) {
        let results:[SCNHitTestResult] = (sceneView?.hitTest(gesture.location(ofTouch: 0, in: sceneView), options: nil))!
        guard let firstNode = results.first else{
            return
        }
        // 点击到的节点
        print(firstNode.node)
    }
    
    var sceneView: SCNView?
    func loadModelFile() {
        
        //设置摄像机架设点
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3(0, 0, 50)
        cameraNode.camera?.zFar = 4000
        
        //设置光源点
        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light?.type = .omni
        lightNode.position = SCNVector3(0, 10, 10)
        
        //设置周围环境
        let ambientLightNode = SCNNode()
        ambientLightNode.light = SCNLight()
        ambientLightNode.light?.type = .ambient
        ambientLightNode.light?.color = UIColor.lightGray
        
        let scene = SCNScene()
        scene.rootNode.addChildNode(cameraNode)
        scene.rootNode.addChildNode(lightNode)
        scene.rootNode.addChildNode(ambientLightNode)
        
        sceneView = SCNView(frame: CGRect.zero)
        sceneView?.translatesAutoresizingMaskIntoConstraints = false
        sceneView?.scene = scene
        sceneView?.allowsCameraControl = true
        //threeDView?.showsStatistics = true
        sceneView?.backgroundColor = UIColor.gray
        view.addSubview(sceneView!)
        
        
        sceneView?.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        sceneView?.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        sceneView?.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        sceneView?.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }


    
    
}
